package com.lanou.beautifulwardrobe.Brand.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.Application.BaseApplication;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity.BrandRankActivity;
import com.lanou.beautifulwardrobe.activity.MainActivity;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.RiseData;
import com.lanou.beautifulwardrobe.entify.RiseItem;

import java.io.Serializable;
import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class RGridViewAdapter extends BaseAdapter {


    private List<RiseData> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private Context context;

    public void addData(List<RiseData> riseItems) {
        datas = riseItems;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() - 2 : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, final ViewGroup parent) {
        Holder holder;
        if (view == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.brand_ranking_item, null);

            holder = new Holder();
            holder.brandTitleImage = (ImageView) view.findViewById(R.id.brandTitleImage);
            holder.brandTitle = (TextView) view.findViewById(R.id.brandTitle);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        if (datas != null && datas.size() > 0) {
            holder.brandTitle.setText(datas.get(position + 2).getGroup_name());
            getBitmap(datas.get(position + 2).getGroup_pic_url(), holder.brandTitleImage);
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(parent.getContext(), BrandRankActivity.class);
                if (datas != null && datas.size() > 0) {
                   //   Toast.makeText(BaseApplication.getContext(), position + 2 + "恭喜!", Toast.LENGTH_SHORT).show();
                    intent.putExtra("arr", (Serializable) datas.get(position + 2).getItems());
                    intent.putExtra("title",datas.get(position+2).getGroup_name());
                    parent.getContext().startActivity(intent);
                }
            }
        });
        return view;
    }

    private class Holder {
        private ImageView brandTitleImage;
        private TextView brandTitle;

    }

    private void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }
}
